﻿@using Reactive.Bindings
@using Reactive.Bindings.Extensions
@using Reactive.Bindings.Components
@using System.Reactive.Linq
@using System.Reactive.Disposables
@using System.Reactive.Concurrency
@page "/"
@implements IDisposable
@inject ValidationViewModel ValidationViewModel
@inject HelloWorldViewModel HelloWorldViewModel

<h1>ReactiveProperty samples</h1>

<h3>Hello world</h3>

<input class="form-control" type="text" @bind="HelloWorldViewModel.Input.Value" @bind:event="oninput" />
<div class="alert alert-primary">
    @HelloWorldViewModel.Output.Value
</div>

<h3>Validation sample</h3>

@*<EditForm Model="ValidationViewModel"
          OnInvalidSubmit="() => ValidationViewModel.InvalidSubmitCommand.ExecuteAsync()"
          OnValidSubmit="() => ValidationViewModel.SubmitCommand.ExecuteAsync()">
*@<EditForm Model="ValidationViewModel"
          OnInvalidSubmit="() => ValidationViewModel.InvalidSubmitCommand.ExecuteAsync()"
          OnValidSubmit="() => ValidationViewModel.SubmitCommand.ExecuteAsync()">
    <ReactivePropertiesValidator />

    <ValidationSummary />

    <div class="mb-3">
        <label for="firstName">First name</label>
        <InputText @bind-Value="ValidationViewModel.FirstName.Value" class="form-control" />
        <ValidationMessage For="() => ValidationViewModel.FirstName.Value" />
    </div>
    <div class="mb-3">
        <label for="firstName">Last name</label>
        <InputText @bind-Value="ValidationViewModel.LastName.Value" class="form-control" />
        <ValidationMessage For="() => ValidationViewModel.LastName.Value" />
    </div>

    <div class="mb-3">
        <span>Full name: @ValidationViewModel.FullName.Value</span>
    </div>

    <button type="submit" class="btn btn-primary" disabled="@ValidationViewModel.SubmitCommand.IsDisabled()">Submit</button>
</EditForm>

@if (!string.IsNullOrEmpty(ValidationViewModel.Message.Value))
{
    <div class="alert alert-primary">
        <span>@ValidationViewModel.Message.Value</span>
    </div>
}

@code {
    private readonly CompositeDisposable _disposable = new();

    protected override void OnInitialized()
    {
        ValidationViewModel.AddTo(_disposable);
        HelloWorldViewModel.AddTo(_disposable);
    }

    public void Dispose() => _disposable.Dispose();
}
